package com.woniu.car.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.car.clientdto.ServiceDetailDto;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.entity.TStatisticsShop;
import com.woniu.car.exception.StatisticsException;
import com.woniu.car.feign.PlatformClient2Service;
import com.woniu.car.parameter.DetailQueryParam;
import com.woniu.car.parameter.StatisticsShopParameter;
import com.woniu.car.properties.StatisticsProperties;
import com.woniu.car.service.TStatisticsShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuyanzu
 * @since 2021-06-22
 */
@RestController
@RequestMapping
@Api(tags = "统计商店服务收益控制层")
public class TStatisticsShopController {
    @Autowired
    private TStatisticsShopService shopService;
    @Resource
    private PlatformClient2Service client;
//   @GetMapping("/StatisticsShop")
//   @ApiOperation("统计商店服务收益")
//    public ResponseEntity StatisticsShop(@Validated StatisticsShopParameter parameter) throws StatisticsException {
//       return shopService.StatisticsShop(parameter);
//   }

        @GetMapping("/queryDetailByStatus")
        @ApiOperation("查询商店服务数据收益")
        public ResponseEntity queryDetailByStatus(@Validated DetailQueryParam detailQueryParam) throws StatisticsException {
            //不能查询当日数据
//            if (detailQueryParam.getStartTime().after(new Date()))
//                throw new StatisticsException(StatisticsProperties.NOSEARCHTODAY);
            if(detailQueryParam.getStartTime().after(detailQueryParam.getEndTime()))
                throw new StatisticsException("开始时间不能大于结束时间");
            QueryWrapper<TStatisticsShop> wrapper = new QueryWrapper<>();
            wrapper.eq("shop_id",detailQueryParam.getShopId())
                    .ge("time",detailQueryParam.getStartTime())
                    .le("time",detailQueryParam.getEndTime());
            List<TStatisticsShop> list = shopService.list(wrapper);
            if(list.size()==0){
                List<ServiceDetailDto> serviceDetailDtos = client.queryDetailByStatus(detailQueryParam).getObject();
                System.out.println(serviceDetailDtos);
                if (null == serviceDetailDtos)
                    throw new StatisticsException(StatisticsProperties.NOSERVERMSG);
                HashMap<String, ServiceDetailDto> map = new HashMap<>();
                for(ServiceDetailDto entity:serviceDetailDtos){
            //遍历 同一天数搜索条件 同一个商铺 同一个服务 总共金额
            //将同一个商铺同一天的所有服务存入map集合
                    String serverName = entity.getServerName();
                    if(!map.containsKey(serverName)){
                            map.put(serverName,entity);
                    }else{
                        ServiceDetailDto dto = map.get(serverName);
                        dto.setTotal(entity.getTotal()+dto.getTotal());
                        map.put(serverName,dto);
                    }
                }
                for(String key:map.keySet()){
                    ServiceDetailDto dto = map.get(key);
                    TStatisticsShop shop = new TStatisticsShop();
                    shop.setShopId(dto.getShopId());
                    shop.setMoney(dto.getTotal());
                    shop.setServerName(dto.getServerName());
                    shop.setTime(dto.getCreateTime());
                    shopService.save(shop);
                }
                return new ResponseEntity()
                        .setObject(serviceDetailDtos)
                        .setCode(StatisticsProperties.SUCCESSCODE)
                        .setMsg(StatisticsProperties.SUCCESSMSG);
            }else{
                return new ResponseEntity()
                        .setObject(list)
                        .setCode(StatisticsProperties.SUCCESSCODE)
                        .setMsg(StatisticsProperties.SUCCESSMSG);
            }
            
        }
    
}

